package com.ruoyi.project.jxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jxy.domain.Entry;
import com.ruoyi.project.jxy.service.IEntryService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 词条Controller
 *
 * @author ruoyi
 * @date 2019-12-05
 */
@RestController
@RequestMapping("/jxy/entry")
public class EntryController extends BaseController {
    @Autowired
    private IEntryService entryService;

    /**
     * 查询词条列表
     */
    @PreAuthorize("@ss.hasPermi('jxy:entry:list')")
    @GetMapping("/list")
    public TableDataInfo list(Entry entry) {
        startPage();
        QueryWrapper<Entry> queryWrapper = new QueryWrapper<Entry>();
        if (StringUtils.isNotBlank(entry.getAlias())) {
            queryWrapper.like("alias", entry.getAlias());
        }
        if (StringUtils.isNotBlank(entry.getName())) {
            queryWrapper.like("name", entry.getName());
        }
        queryWrapper.eq("entity_id", entry.getEntityId());
        queryWrapper.eq("create_user", SecurityUtils.getUsername());
        queryWrapper.orderByDesc("id");
        List<Entry> list = entryService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 获取词条详细信息
     */
    @PreAuthorize("@ss.hasPermi('jxy:entry:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(entryService.getById(id));
    }

    /**
     * 新增词条
     */
    @PreAuthorize("@ss.hasPermi('jxy:entry:add')")
    @Log(title = "词条", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Entry entry) {
        if (entry.getEntityId() == null) {
            return AjaxResult.error("实体id不能为空!");
        }
        if (StringUtils.isBlank(entry.getName()) && StringUtils.isBlank(entry.getAlias())) {
            return AjaxResult.error("名称或别名不能为空");
        }
        QueryWrapper<Entry> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(entry.getName())) {
            queryWrapper.eq("name", entry.getName());
        } else {
            queryWrapper.eq("alias", entry.getAlias());
        }
        queryWrapper.eq("entity_id", entry.getEntityId());
        int count = entryService.count(queryWrapper);
        if (count > 0) {
            return AjaxResult.error("词条已存在!");
        }
        entry.setCreateTime(new Date());
        entry.setCreateUser(SecurityUtils.getUsername());
        return toAjax(entryService.save(entry) ? 1 : 0);
    }

    /**
     * 修改词条
     */
    @PreAuthorize("@ss.hasPermi('jxy:entry:edit')")
    @Log(title = "词条", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Entry entry) {
        entry.setUpdateTime(new Date());
        entry.setUpdateUser(SecurityUtils.getUsername());
        return toAjax(entryService.updateById(entry) ? 1 : 0);
    }

    /**
     * 删除词条
     */
    @PreAuthorize("@ss.hasPermi('jxy:entry:remove')")
    @Log(title = "词条", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(entryService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
